package hivi.vn.hivigold.adapters;

import hivi.vn.hivigold.R;
import hivi.vn.hivigold.objects.Gold;
import hivi.vn.hivigold.utils.Formats;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class GoldPriceAdapter extends BaseAdapter {
	ArrayList<Gold> arr;
	Context context;
	public GoldPriceAdapter(Context context, ArrayList<Gold> arr) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.arr = arr;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arr==null?0:arr.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arr==null?null:arr.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		View v = arg1;
		Holder h;
		if(v == null)
		{
			v =( (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.pricerow_layout, null);
			h = new Holder();
			h.tvName = (TextView)v.findViewById(R.id.tvTienteID);
			h.tvMua = (TextView)v.findViewById(R.id.tvMuavao);
			h.tvBan = (TextView)v.findViewById(R.id.tvBanRa);
			h.tvNguoiSua = (TextView)v.findViewById(R.id.tvUserID2);
			h.tvNgaySua = (TextView)v.findViewById(R.id.tvDatetime2);
			v.setTag(h);
		}else
		{
			h = (Holder)v.getTag();
		}
		if (arr!=null)
		{
			Gold g = arr.get(arg0);
			h.tvName.setText(g.getTienteName());
			h.tvMua.setText(Formats.money(g.getMua_vao()));
			h.tvBan.setText(Formats.money(g.getBan_ra()));
			h.tvNguoiSua.setText(g.getUser_id2());
			h.tvNgaySua.setText(g.getDatetime2());	
		}
		return v;
	}

	public void swaper(ArrayList<Gold> arr)
	{
		this.arr = arr;
		notifyDataSetChanged();
	}
	
	public Gold getGoldAtIndex(int index)
	{
		return arr.get(index);
	}
	
	static class Holder
	{
		TextView tvName;
		TextView tvMua;
		TextView tvBan;
		TextView tvNguoiSua;
		TextView tvNgaySua;
	}
}
